package pers.rcl.utils;

public class Calculate {
	// 运算数栈顶指针
	private static int stackOneTop = -1;
	// 运算符栈顶指针
	private static int stackTwoTop = -1;

	private static String temp1 = null;
	private static String temp2 = null;

	private static double test = 0.00;

	private static int priority = 0;

	public double calculateResult(String caRule) {
		// System.out.println("程序开始-----------------");
		char[] doFormula = new char[caRule.length()];
		doFormula = caRule.toCharArray();
		// 运算数栈
		double[] stackOne = new double[1000];
		// 运算符栈
		int[] stackTwo = new int[1000];

		try {
			System.out.println("第一个for循环开始---------doFormula.length" + doFormula.length);
			for (int i = 0; i < doFormula.length; i++) {
				priority = 0;
				switch (doFormula[i]) {
				case '+':
					priority = 1;
					break;
				case '-':
					priority = 2;
					break;
				case '*':
					priority = 3;
					break;
				case '/':
					priority = 4;
					break;
				case '(':
					priority = 5;
					break;
				case ')':
					priority = 6;
					break;
				}
				if (priority == 0) {
					if (temp1 == null) {
						temp1 = String.valueOf(doFormula[i]);
					} else {
						temp1 = temp1 + doFormula[i];
					}
				} else if (priority == 5) {
					stackTwo[++stackTwoTop] = priority;
					priority = 0;
				} else if (priority == 6) {
					stackOne[++stackOneTop] = Double.parseDouble(temp1);
					temp1 = null;
					while (stackTwo[stackTwoTop] != 5) {
						double one = stackOne[stackOneTop--];
						double two = stackOne[stackOneTop--];
						switch (stackTwo[stackTwoTop--]) {
						case 1:
							test = two + one;
							break;
						case 2:
							test = two - one;
							break;
						case 3:
							test = one * two;
							break;
						case 4:
							test = two / one;
							break;
						}
						stackOne[++stackOneTop] = test;
						System.out.println("后括号计算值" + test);
					}
					stackTwoTop--;
				} else {
					if (stackTwoTop == -1) {
						if (stackOneTop == -1) {
							stackOne[++stackOneTop] = Double.parseDouble(temp1);
							temp1 = null;
						}
						stackTwo[++stackTwoTop] = priority;
						priority = 0;
					} else {

						if (stackTwo[stackTwoTop] > 1 && stackTwo[stackTwoTop] < 5 && stackTwo[stackTwoTop] >= priority) {
							stackOne[++stackOneTop] = Double.parseDouble(temp1);
							temp1 = null;
							System.out.println("开始计算-----------------------");
							calculate(stackOne, stackTwo);
							System.out.println("结束计算-----------------------");
							stackTwo[++stackTwoTop] = priority;
							priority = 0;
						} else {
							if (temp1 == null) {
								stackTwo[++stackTwoTop] = priority;
								priority = 0;
							} else {
								stackOne[++stackOneTop] = Double.parseDouble(temp1);
								temp1 = null;
								stackTwo[++stackTwoTop] = priority;
								priority = 0;
							}
						}
					}
				}

				if (i + 1 == doFormula.length && temp1 != null) {
					stackOne[++stackOneTop] = Double.parseDouble(temp1);
					if (stackTwo[stackTwoTop] > 1) {
						calculate(stackOne, stackTwo);
					}
				}
			}

			System.out.println("结束第一个for循环----------------");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		System.out.println("打印计算数：");
		for (int j = 0; j <= stackOneTop; j++) {
			System.out.println("第" + j + "个数为：" + stackOne[j]);
		}
		System.out.println("打印计算符");
		for (int k = 0; k <= stackTwoTop; k++) {
			System.out.println("第" + k + "个数为：" + stackTwo[k]);
		}

		if (stackOneTop != stackTwoTop + 1) {
			System.out.println("计算数为：" + stackOneTop);
			System.out.println("计算符个数为：" + stackTwoTop);
			System.out.println("意外结束----------------");
			return 0.00;
		}

		System.out.println("进入第二个while循环--------------");
		while (stackTwoTop != -1) {
			double one = stackOne[stackOneTop--];
			double two = stackOne[stackOneTop--];
			switch (stackTwo[stackTwoTop--]) {
			case 1:
				test = two + one;
				break;
			case 2:
				test = two - one;
				break;
			case 3:
				test = one * two;
				break;
			case 4:
				test = two / one;
				break;
			}
			stackOne[++stackOneTop] = test;
		}
		System.out.println("结束第二个while循环-------------");

		System.out.println("最终结果为：" + stackOne[stackOneTop]);
		return stackOne[stackOneTop];
	}

	public void calculate(double[] stackOne, int[] stackTwo) {
		double temp3 = 0.00;
		if (stackTwoTop == -1) {
			System.out.println("计算时退出");
			return;
		}
		while (stackTwo[stackTwoTop] > 1) {
			double one = stackOne[stackOneTop--];
			double two = stackOne[stackOneTop--];
			switch (stackTwo[stackTwoTop--]) {
			case 1:
				temp3 = two + one;
				break;
			case 2:
				temp3 = two - one;
				break;
			case 3:
				temp3 = two * one;
				break;
			case 4:
				temp3 = two / one;
				break;
			}
			stackOne[++stackOneTop] = temp3;
			if (stackTwoTop == -1) {
				break;
			}
		}
	}
}
